#ifndef __I386_HW_IO_H__
#define __I386_HW_IO_H__

#ifdef __cplusplus
extern "C" {
#endif

inline static unsigned char inp(unsigned short _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0"
	  : "=a" (rv)
	  : "d" (_port));
    return(rv);
}

inline static unsigned short inpw (unsigned short _port) {
    unsigned short rv;
    __asm__ __volatile__ ("inw %1, %0"
	  : "=a" (rv)
	  : "d" (_port));
    return(rv);
}

inline static unsigned int inpd(unsigned short _port) {
    unsigned int rv;
    __asm__ __volatile__ ("inl %1, %0"
	  : "=a" (rv)
	  : "d" (_port));
    return(rv);
}

inline static void outp(unsigned short _port, unsigned char _data) {
    __asm__ __volatile__ ("outb %1, %0"
	  :
	  : "d" (_port),
	    "a" (_data));
}

inline static void outpw(unsigned short _port, unsigned short _data) {
    __asm__ __volatile__ ("outw %1, %0"
	  :
	  : "d" (_port),
	    "a" (_data));
}

inline static void outpd(unsigned short _port, unsigned long _data) {
    __asm__ __volatile__ ("outl %1, %0"
	  :
	  : "d" (_port),
	    "a" (_data));
}

inline static void inprep(unsigned short _port, unsigned char *_buffer,
	unsigned int _reads) {
	__asm__ __volatile__ ("pushal \n\t"	
		"pushfl \n\t"	
		"cli \n\t"	
		"cld \n\t"	
		"rep insb \n\t"	
		"popfl \n\t"	
		"popal"	
		:
		: "d" (_port),
		  "D" (_buffer),
		  "c" (_reads));
}

inline static void outprep(unsigned short _port, unsigned char *_buffer,
	unsigned int _writes) {
	__asm__ __volatile__ ("pushal \n\t"
		"pushfl \n\t"
		"cli \n\t"
		"cld \n\t"
		"rep outsb \n\t"
		"popfl \n\t"
		"popal"
		:
		: "d" (_port),
		  "S" (_buffer),
		  "c" (_writes));
}

inline static void inpwrep(unsigned short _port, unsigned short *_buffer,
	unsigned int _reads) {
	__asm__ __volatile__ ("pushal \n\t"	
		"pushfl \n\t"	
		"cli \n\t"	
		"cld \n\t"	
		"rep insw \n\t"	
		"popfl \n\t"	
		"popal"	
		:
		: "d" (_port),
		  "D" (_buffer),
		  "c" (_reads));
}

inline static void outpwrep(unsigned short _port, unsigned short *_buffer,
	unsigned int _writes) {
	__asm__ __volatile__ ("pushal \n\t"
		"pushfl \n\t"
		"cli \n\t"
		"cld \n\t"
		"rep outsw \n\t"
		"popfl \n\t"
		"popal"
		:
		: "d" (_port),
		  "S" (_buffer),
		  "c" (_writes));
}

inline static void inpdrep(unsigned short _port, unsigned int *_buffer,
	unsigned int _reads) {
	__asm__ __volatile__ ("pushal \n\t"	
		"pushfl \n\t"	
		"cli \n\t"	
		"cld \n\t"	
		"rep insl \n\t"	
		"popfl \n\t"	
		"popal"	
		:
		: "d" (_port),
		  "D" (_buffer),
		  "c" (_reads));
}

inline static void outpdrep(unsigned short _port, unsigned int *_buffer,
	unsigned int _writes) {
	__asm__ __volatile__ ("pushal \n\t"
		"pushfl \n\t"
		"cli \n\t"
		"cld \n\t"
		"rep outsl \n\t"
		"popfl \n\t"
		"popal"
		:
		: "d" (_port),
		  "S" (_buffer),
		  "c" (_writes));
}

#ifdef __cplusplus
}
#endif

#endif
